package com.zynn.service.module.es.service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.plugins.Page;
import com.zynn.common.core.utils.DateUtils;
import com.zynn.service.module.es.entity.Events;
import com.zynn.service.module.es.entity.Groups;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 刘天元
 * @description
 * @create 2018-07-01 13:26
 **/
@Slf4j
@Service
public class InitAllEventService {


    @Autowired
    private SyncEventService syncEventService;

    @Autowired
    private EventsService eventsService;

    @Autowired
    private CommentsService commentsService;

    @Autowired
    private LikeService likeService;

    @Autowired
    private PicturesService picturesService;

    @Autowired
    private GroupsService groupsService;

    public void sync(Integer current, boolean isAsc) {
        if (current == null) {
            current = 1;
        }
        int size = 1000;
        while (true) {
            log.info("动态-------------------" + Thread.currentThread().getName() + ",current:" + current);
            Page<Events> page = eventsService.selectPage(new Page<>(current, size, "eid", isAsc));
            List<Events> eventsList = page.getRecords();

            syncEventsList(eventsList);

            if (eventsList.size() < size) {
                log.info("动态结束-------------------" + eventsList.size());
                break;
            }
            current = current + 1;
        }
    }

    private void syncEventsList(List<Events> eventsList) {
        for (Events events : eventsList) {
            int likeCount = likeService.selectCount(Condition.create().eq("likeEventID", events.getEid()));
            int picturesCount = picturesService.selectCount(Condition.create().eq("peid", events.getEid()));
            int commentCount = commentsService.selectCount(Condition.create().eq("ceid", events.getEid()));
            Groups groups = groupsService.selectById(events.getEgroupid());
            Integer groupNewType = groups.getGroupNewType();
            int groupTypeValue = 1;
            if (groupNewType != null) {
                if (groupNewType == 3) {
                    groupTypeValue = 2;
                }
                if (groupNewType == 4) {
                    groupTypeValue = 5;
                }
                if (groupNewType == 5) {
                    groupTypeValue = 3;
                }
                if (groupNewType == 11) {
                    groupTypeValue = 4;
                }
                if (groupNewType == 30) {
                    groupTypeValue = 90;
                }
                if (groupNewType == 35) {
                    groupTypeValue = 91;
                }
                if (groupNewType == 40) {
                    groupTypeValue = 92;
                }
                if (groupNewType == 50) {
                    groupTypeValue = 10;
                }
            }
            if (events.getEstatus() == 0) {
                syncEventService.syncOneEvent(events.getEid().longValue(), events.getEMain(), events.getEuserid().longValue(), events.getEgroupid().longValue(), events.getEgroupid().toString(), groupTypeValue,
                        events.getEplace(), events.getTextTitle(), events.getEtext(), DateUtils.format(events.getEuploadtime()), null, null, commentCount, likeCount, picturesCount);


            }
            if (events.getEstatus() == 1) {
                syncEventService.remove(events.getEid().longValue());
            }

        }
    }

    public void init(Long start, Long end, boolean isAsc) {
        if (start == null) {
            start = 0L;
        }
        if (end == null) {
            end = Long.MAX_VALUE;
        }
        int size = 1000;
        while (true) {
            List<Events> eventsList = eventsService.selectList(
                    Condition.create()
                            .le("eid", end)
                            .ge("eid", start)
                            .orderBy("eid", isAsc)
                            .last("limit " + size)
            );
            syncEventsList(eventsList);

            if (eventsList.size() < size) {
                log.info("动态结束-------------------" + eventsList.size());
                break;
            }

            if (isAsc) {
                start = eventsList.get(size - 1).getEid().longValue();
            } else {
                end = eventsList.get(size - 1).getEid().longValue();
            }

            log.info("动态start-------------------{}", start);
            log.info("动态end-------------------{}", end);

        }
    }

}
